<template>
  <div class="main-content">
    <el-row>
      <el-button
        size="medium"
        class="filter-item"
        type="primary"
        @click="handleCreate"
        >新增公司部门</el-button
      >
      <el-button
        size="medium"
        class="filter-item"
        type="primary"
        @click="importComfirm"
        >导入公司部门</el-button
      >
    </el-row>

    <p class="maintitle">公司组织列表</p>
    <el-table :data="tableData" border>
      <el-table-column type="selection" width="50"> </el-table-column>
      <el-table-column type="index" label="ID" align="center">
      </el-table-column>
      <el-table-column
        prop="dptmt_name"
        label="总部门名称"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="org_name"
        label="部门组织名称"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="statusInfo"
        label="部门状态"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" @click="handleEdit(row)"
            >编辑</el-button
          >
          <el-popconfirm
            confirm-button-text="确认"
            cancel-button-text="取消"
            title="确认修改状态？"
            @confirm="changeStatus(row.id, row.status)"
            style="margin-left: 10px"
          >
            <el-button size="mini" type="text" slot="reference"
              >更改部门状态</el-button
            >
          </el-popconfirm>
          <el-popconfirm
            confirm-button-text="确认"
            cancel-button-text="取消"
            title="确认删除当前组织？"
            @confirm="deleteRow(row.id)"
            style="margin-left: 10px"
          >
            <el-button size="mini" type="text" slot="reference">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      style="text-align: right"
      v-show="totalPage > 0"
      :total="totalPage"
      :page.sync="getListData.pageNo"
      :limit.sync="getListData.pageSize"
      @pagination="getList"
    ></pagination>

    <el-dialog title="编辑" :visible.sync="dialogVisible" width="100">
      <el-form
        :model="form"
        ref="acForm"
        label-position="left"
        label-width="150px"
        :inline="true"
      >
        <el-row>
          <el-form-item
            label="总部门名称"
            prop="dptmt_name"
            :rules="[{ required: true, message: '总部门名称不能为空' }]"
          >
            <!-- <el-input clearable v-model="form.dptmt_name"></el-input> -->

            <el-select
              v-model="form.dptmt_name"
              filterable
              allow-create
              default-first-option
              placeholder="请选择总部门"
            >
              <el-option
                v-for="item in dptmt"
                :key="item.dptmt_name"
                :label="item.dptmt_name"
                :value="item.dptmt_name"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="部门组织名称"
            prop="org_name"
            :rules="[{ required: true, message: '部门组织名称不能为空' }]"
          >
            <el-input clearable v-model="form.org_name"></el-input>
          </el-form-item>
        </el-row>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="dialogStatus === 'create' ? updateData('') : updateData()"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 导入 -->
    <el-dialog
      title="导入"
      v-loading.fullscreen.lock="loading"
      element-loading-text="正在导入,请稍后..."
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.8)"
      :visible.sync="importVisible"
      width="30%"
      :close-on-click-modal="false"
    >
      <el-button
        size="medium"
        type="success"
        style="margin-bottom: 10px"
        @click="templateDownload"
        >模板下载</el-button
      >
      <el-upload
        class="upload-demo"
        drag
        action="#"
        :multiple="false"
        :limit="1"
        :auto-upload="false"
        ref="upload"
        :http-request="importPattern"
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">
          *只能上传一个文件，若需修改请删除原文件后重新上传
        </div>
      </el-upload>
      <span slot="footer" class="dialog-footer">
        <el-button @click="importVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmImport">导入</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { Message } from "element-ui";
import {
  organizeList,
  organizeDelete,
  organizeImport,
  organizeStatus,
  organizeEdit,
  dptmtList,
} from "@/app/api/system";
import { mapState } from "vuex";
import Pagination from "@/app/components/Pagination";

export default {
  components: {
    Pagination,
  },
  name: "Organize",
  data() {
    return {
      importVisible: false,
      loading: false,
      tableData: [],
      totalPage: 1,
      listLoading: true,
      getListData: {
        pageNo: 1,
        pageSize: 10,
      },
      dialogVisible: false,
      form: {},
      dialogStatus: "", //编辑或新增
      textMap: {
        update: "编辑",
        create: "新增",
      },
      baseurl: api_url,
      dptmt: [],
    };
  },
  computed: {
    ...mapState("user", ["session", "account"]),
  },
  created() {
    this.getList();
    this.dptmtList();
  },
  methods: {
    importComfirm() {
      this.importVisible = true;
      this.$nextTick(() => {
        this.$refs["upload"].clearFiles();
      });
    },
    confirmImport() {
      this.loading = true;
      this.$nextTick(() => {
        this.$refs.upload.submit();
      });
    },
    importPattern(param) {
      const fileObj = param.file;
      const form = new FormData();
      form.append("file", fileObj);
      organizeImport(form)
        .then((res) => {
          console.log("import_score res", res);
          Message({
            message: "导入成功",
            type: "success",
            duration: 2 * 1000,
          });
          this.loading = false;
          this.importVisible = false;
          this.getList();
        })
        .catch((error) => {
          console.log("import_score error", error);
          this.loading = false;
          this.importVisible = false;
        });
    },
    templateDownload() {
      window.location.href = this.baseurl + "/template/organize.xlsx";
    },
    getList() {
      organizeList(this.getListData)
        .then((res) => {
          this.tableData = res.data.ret;
          this.totalPage = res.data.count;
          this.listLoading = false;
        })
        .catch((error) => {
          console.log("studentSourceList error", error);
        });
    },

    dptmtList() {
      dptmtList()
        .then((res) => {
          this.dptmt = res.data;

          console.log(this.dptmt);
        })
        .catch((error) => {
          console.log("dptmtList error", error);
        });
    },
    deleteRow(oid) {
      organizeDelete({ oid })
        .then((res) => {
          if (res.code == "00000") {
            Message({
              message: "删除成功",
              type: "success",
              duration: 5 * 1000,
            });
            this.getList();
          }
        })
        .catch((error) => {
          console.log("save error", error);
        });
    },
    handleCreate() {
      this.form = {
        dptmt_name: "",
        org_name: "",
      };
      this.dialogStatus = "create";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["acForm"].clearValidate();
      });
    },
    updateData(id) {
      this.$refs["acForm"].validate((valid) => {
        if (valid) {
          if (id) {
            this.form.id = id;
          }
          organizeEdit(this.form)
            .then((res) => {
              if (res.code == "00000") {
                Message({
                  message: "操作成功",
                  type: "success",
                  duration: 2 * 1000,
                });
                this.getList();
                this.dptmtList();
                this.dialogVisible = false;
              }
            })
            .catch((error) => {
              console.log("editOrder error", error);
            });
        } else {
          return false;
        }
      });
    },
    handleEdit(row) {
      this.form = Object.assign({}, this.form, row);
      console.log(this.form);
      this.dialogStatus = "update";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["acForm"].clearValidate();
      });
    },
    changeStatus(oid, curStatus) {
      const status = curStatus == 1 ? 0 : 1;
      organizeStatus({ oid, status })
        .then((res) => {
          if (res.code == "00000") {
            Message({
              message: "修改成功",
              type: "success",
              duration: 5 * 1000,
            });
            this.getList();
          }
        })
        .catch((error) => {
          console.log("save error", error);
        });
    },
  },
};
</script>

<style>
.main-content {
  width: 97%;
  height: 840px;
  margin: 1% auto;
  border-radius: 10px;
  box-shadow: 7px 7px 10px 0px rgba(0, 0, 0, 0.25);
  background-color: #fbfbfb;
  padding: 0px;
  position: relative;
}
.maintitle {
  font-family: Roboto;
  font-style: normal;
  font-weight: 500;
  font-size: 20px;
  line-height: 22px;
  color: #000000;
  margin: 10px 0 20px 10px;
}
</style>
